home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / VideoToolbox 95.04.18 / VideoToolboxSources / PrintfExit.c < prev    next >
Text File  |  1995-03-19  |  2KB  |  81 lines

  1. /* 
  2. PrintfExit.c
  3.  
  4. PrintfExit(const char *format,...);
  5. is normally equivalent to calling printf and exit.
  6.  
  7. Lots of VideoToolbox routines, when they find a really grievous error, print out
  8. a message and exit. Having one routine that does both makes the code slightly
  9. neater, since the if statement then doesn't need braces. Furthermore, both of
  10. these functions, printf, and exit are liable to break in foreign environments,
  11. e.g. when running as a MEX resource under MATLAB. Now the problem is confined to
  12. this file, where it can be handled by conditional compilation.
  13.  
  14. Since PrintfExit is called only when we're near death, it seems prudent to make
  15. sure there's enough stack space before calling printf, which crashes if there's
  16. less than about 4500 byts of stack. Note that StackGrow() moves memory.
  17.  
  18. I've replaced all calls to exit() in the entire VideoToolbox by calls to
  19. PrintfExit().
  20.  
  21. StackGrow() is defined in VideoToolbox.h.
  22.  
  23. HISTORY:
  24. 2/20/93    dgp    Wrote it based on conversation with David Brainard.
  25. 7/9/93    dgp    Test MATLAB in #if instead of #ifdef.
  26. 9/12/93    dgp    Moved Required() to Require.c.
  27. 9/15/93    dgp    Added "const" to prototype.
  28. */
  29. #include "VideoToolbox.h"
  30. #include <stdarg.h>            /* for variable-number-of-argument macros */
  31. #if __MWERKS__
  32.     #include <SIOUX.h>
  33. #endif
  34.  
  35. int PrintfExit(const char *format,...)
  36. {
  37.     va_list args;
  38.     int i;
  39.     long qD=0;
  40.   
  41.     #if MAC_C
  42.         /*
  43.         The main program may have changed the current device. Let's
  44.         restore the main device before doing the printf.
  45.         */
  46.         Gestalt(gestaltQuickdrawVersion,&qD);
  47.         if(qD>=gestalt8BitQD)SetGDevice(GetMainDevice());
  48.         if(StackSpace()<6000)StackGrow(6000-StackSpace());
  49.     #endif
  50.     #if __MWERKS__
  51.         SIOUXSettings.autocloseonquit=0;
  52.     #endif
  53.     #if !MATLAB
  54.         #if MAC_C
  55.             /* printf crashes if there's less than about 4500 bytes of stack space */
  56.             if(StackSpace()<5000){
  57.                 SysBeep(20);
  58.                 exit(EXIT_FAILURE);
  59.             }
  60.         #endif
  61.         va_start(args,format);
  62.         i=vfprintf(stdout,format,args);
  63.         va_end(args);
  64.         #if __MWERKS__
  65.             printf("Hit Command-Q to quit.\n");
  66.         #endif
  67.         exit(EXIT_FAILURE);
  68.     #else
  69.     {
  70.         char s[256];
  71.         
  72.         va_start(args,format);
  73.         i=vsprintf(s,format,args);
  74.         va_end(args);
  75.         mex_error(s);    // Ask MATLAB to report the error.
  76.     }
  77.     #endif
  78.     return 0;            // can't get here
  79. }
  80.  
  81.